public class ThreadDemo2 {
    // **** 线程的中断 ****

    private static boolean flag = true;
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while(flag){
                System.out.println("hello thread");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 在主线程里就可以随时通过 flag 变量的取值， 来操作thread线程是否结束。
        flag = false;
    }
}
